package com.fms.trollalarm.custom;

import java.util.Random;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

public class MoveAnimation {

	// private OnClickViewListener listener;
	private int lastXPosition = 0;
	private int lastYposition = 0;
	private int xMax = 0;
	private int yMax = 0;
	private int xMin = 0;
	private int yMin = 0;
	private View view;
	private View parent;
	private MyTranslateAnimation currentTranslateAnimation;
	private long durationPerMoving;
	private Context context;
	private int numMove = 0;
	private int countMove = 0;
	private OnMoveAnimationListener listener;

	@SuppressLint("NewApi")
	public MoveAnimation(View view, View parent, Context context) {
		super();
		this.view = view;
		this.parent = parent;
		// xMin = parent.getLeft() + parent.getPaddingLeft();
		// yMin = parent.getTop() + parent.getPaddingTop();
		// xMax = parent.getRight() - view.getWidth() -
		// parent.getPaddingRight();
		// yMax = parent.getBottom() - view.getHeight()
		// - parent.getPaddingBottom();

		xMin = parent.getPaddingLeft();
		yMin = parent.getTop() + parent.getPaddingTop();
		xMax = parent.getRight() - view.getWidth() - parent.getPaddingRight();
		yMax = parent.getBottom() - view.getHeight()
				- parent.getPaddingBottom();
		this.context = context;
		// setEvent();
	}

	// private void setEvent() {
	// view.setOnClickListener(new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// if (listener != null) {
	// listener.onClick(v);
	// }
	// }
	// });
	//
	// }

	public void start(long durationPerMoving, final int numMove) {
		this.numMove = numMove;
		this.durationPerMoving = durationPerMoving;
		int[] location = getRandomPosition(lastXPosition, lastYposition);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			Log.d("fms", "new version (>=honeycomb)");
			view.animate().x(location[0]).y(location[1])
					.setDuration(durationPerMoving)
					.setListener(new AnimatorListener() {

						@Override
						public void onAnimationStart(Animator animation) {
							if (listener != null && countMove == 0) {
								listener.onBegin(view);
							}
						}

						@Override
						public void onAnimationRepeat(Animator animation) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationEnd(Animator animation) {
							countMove++;
							if (numMove == 0 || countMove <= numMove) {
								int[] location = getRandomPosition(
										lastXPosition, lastYposition);
								view.animate().x(location[0]).y(location[1]);
								lastXPosition = location[0];
								lastYposition = location[1];
							} else {
								countMove = 0;
								animation.cancel();
								if (listener != null) {
									listener.onStop(view);
								}
							}

						}

						@Override
						public void onAnimationCancel(Animator animation) {
							// TODO Auto-generated method stub

						}
					});
		} else {
			Log.d("fms", "old version ( < honeycomb)");
			// animate(view).x(location[0]).y(location[1])
			// .setDuration(durationPerMoving)
			// .setListener(customAnimationListener);
			view.startAnimation(getTranslateAnimation());
		}
		this.lastXPosition = location[0];
		this.lastYposition = location[1];
	}

	// private com.nineoldandroids.animation.Animator.AnimatorListener
	// customAnimationListener = new
	// com.nineoldandroids.animation.Animator.AnimatorListener() {
	//
	// @Override
	// public void onAnimationStart(
	// com.nineoldandroids.animation.Animator animation) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void onAnimationRepeat(
	// com.nineoldandroids.animation.Animator animation) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void onAnimationEnd(
	// com.nineoldandroids.animation.Animator animation) {
	// Log.d("fms", "onAnimationEnd!");
	// int[] location = getRandomPosition(lastXPosition, lastYposition);
	// animate(view).x(location[0]).y(location[1])
	// .setListener(customAnimationListener);
	// lastXPosition = location[0];
	// lastYposition = location[1];
	//
	// }
	//
	// @Override
	// public void onAnimationCancel(
	// com.nineoldandroids.animation.Animator animation) {
	// // TODO Auto-generated method stub
	//
	// }
	// };

	public View getView() {
		return view;
	}

	private int[] getRandomPosition(int lastXPosition, int lastYposition) {
		Random rnd = new Random();
		int x = rnd.nextInt(2);
		int[] position = new int[2];
		int xMid = (xMax - xMin) / 2;
		int yMid = (yMax - yMin) / 2;
		if (lastXPosition <= xMid) {
			if (x == 0) {
				position[0] = xMid + rnd.nextInt(xMid) - xMin;
				position[1] = rnd.nextInt(yMax) - yMin;
			} else {
				position[0] = rnd.nextInt(xMid) - xMin;
				if (lastYposition <= yMid) {
					position[1] = yMid + rnd.nextInt(yMid) - yMin;
				} else {
					position[1] = rnd.nextInt(yMid) - yMin;
				}

			}
		} else {
			if (x == 0) {
				position[0] = rnd.nextInt(xMid) - xMin;
				position[1] = rnd.nextInt(yMax) - yMin;
			} else {
				position[0] = xMid + rnd.nextInt(xMid) - xMin;
				if (lastYposition <= yMid) {
					position[1] = yMid + rnd.nextInt(yMid) - yMin;
				} else {
					position[1] = rnd.nextInt(yMid) - yMin;
				}

			}
		}
		return position;
	}

	// public void setOnClickViewListener(OnClickViewListener listener) {
	// this.listener = listener;
	// }
	//
	// public interface OnClickViewListener {
	// public void onClick(View v);
	// }
	private MyTranslateAnimation getTranslateAnimation() {
		int[] location;
		location = getRandomPosition(lastXPosition, lastYposition);
		currentTranslateAnimation = new MyTranslateAnimation(view, parent,
				lastXPosition, location[0], lastYposition, location[1]);
		lastXPosition = location[0];
		lastYposition = location[1];
		currentTranslateAnimation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				if (listener != null && countMove == 0) {
					listener.onBegin(view);
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				countMove++;
				if (numMove == 0 || countMove <= numMove) {
					view.startAnimation(getTranslateAnimation());
				} else {
					countMove = 0;
					animation.cancel();
					if (listener != null) {
						listener.onStop(view);
					}
				}

			}
		});
		currentTranslateAnimation.setRepeatCount(0);
		currentTranslateAnimation.setInterpolator(AnimationUtils
				.loadInterpolator(context,
						android.R.anim.accelerate_interpolator));
		currentTranslateAnimation.setDuration(durationPerMoving);
		currentTranslateAnimation.setFillAfter(false);
		return currentTranslateAnimation;
	}

	public void setOnMoveAnimationListener(OnMoveAnimationListener listener) {
		this.listener = listener;
	}

	public interface OnMoveAnimationListener {
		void onBegin(View v);

		void onStop(View v);
	}
}
